package com.hanthink.system.service.impl.equipment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.hanthink.system.service.pc.IEmTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.utils.DateUtils;
import com.hanthink.system.domain.pc.EmOutgoingQuality;
import com.hanthink.system.domain.pc.EmTask;
import com.hanthink.system.mapper.EmTaskMapper;
import com.hanthink.system.mapper.equipment.EmOutgoingQualityMapper;
import com.hanthink.system.service.equipment.IEmOutgoingQualityService;

/**
 * 设备任务检查Service业务层处理
 * 
 * @author hanthink
 * @date 2021-07-19
 */
@Service
public class EmOutgoingQualityServiceImpl implements IEmOutgoingQualityService {

	@Autowired
	private EmOutgoingQualityMapper emOutgoingQualityMapper;

	@Autowired
	private EmTaskMapper emTaskMapper;

	@Autowired
	private IEmTaskService taskService;

    @Override
    public List<EmOutgoingQuality> selectEmOutgoingQualityListbybatchno(String batchno, String isfinished,String tasktype) {
        return emOutgoingQualityMapper.selectEmOutgoingQualityListbybatchno(batchno,isfinished,tasktype);
    }

    @Override
	public List<EmOutgoingQuality> selectEmOutgoingQualityList(EmOutgoingQuality emOutgoingQuality) {
		return emOutgoingQualityMapper.selectEmOutgoingQualityList(emOutgoingQuality);
	}

	@Override
	public List<EmOutgoingQuality> selectEmOutgoingQualityListQC(String batchno,String isfinished,Long userid) {
		return emOutgoingQualityMapper.selectEmOutgoingQualityListQC(batchno,isfinished,userid);
	}

	@Override
	public List<EmOutgoingQuality> selectEmOutgoingQualityListQCNobatch(String isfinished, Long userID) {
		return emOutgoingQualityMapper.selectEmOutgoingQualityListQCNobatch(isfinished,userID);
	}


	@Override
	public int insertEmOutgoingQuality(EmOutgoingQuality emOutgoingQuality) {
		return emOutgoingQualityMapper.insertEmOutgoingQuality(emOutgoingQuality);
	}

	@Override
	public int updateEmOutgoingQuality(EmOutgoingQuality emOutgoingQuality) {
		if (null != emOutgoingQuality.getIsFinished() && "1".equals(emOutgoingQuality.getIsFinished())) {
			emOutgoingQuality.setEndTime(new Date());
		} else {
			emOutgoingQuality.setStartTime(new Date());
		}
		return emOutgoingQualityMapper.updateEmOutgoingQuality(emOutgoingQuality);
	}

	@Override
	public int updateEmOutgoingQualityQC(EmOutgoingQuality emOutgoingQuality) {
		if (null != emOutgoingQuality.getIsFinished() && "1".equals(emOutgoingQuality.getIsFinished())) {
			emOutgoingQuality.setEndTime(new Date());
		} else {
			emOutgoingQuality.setStartTime(new Date());
		}
		return emOutgoingQualityMapper.updateEmOutgoingQualityQC(emOutgoingQuality);
	}


	@Override
	public AjaxResult extractEmOutgoingQuality(EmOutgoingQuality emOutgoingQuality) {

		try {
			// 抽取当班任务
			if (null != emOutgoingQuality.getBatchNum() && emOutgoingQuality.getBatchNum() > 0) {
				EmTask emTask = new EmTask();
				emTask.setStatus("2");
				Date[] date = DateUtils.getShiftDate();
				emTask.setStartTime(date[0]);
				emTask.setEndTime(date[1]);
				emTask.setSiteKey(emOutgoingQuality.getSiteKey());
				emTask.setHangameKey(emOutgoingQuality.getHangameKey());
				emTask.setTaskNum(emOutgoingQuality.getBatchNum());

				List<EmTask> emTaskList = emTaskMapper.selectRandomEmTaskList(emTask);
				if (emTaskList.size() > 0) {
					// 获取批次
					String uuid = UUID.randomUUID().toString();
					emOutgoingQuality.setBatchNo(uuid);
					emOutgoingQuality.setIsFinished("0");
					for (EmTask task : emTaskList) {
						emOutgoingQuality.setHangameKey(task.getHangameKey());
						emOutgoingQuality.setSiteKey(task.getSiteKey());
						emOutgoingQuality.setTaskId(task.getId());
						emOutgoingQualityMapper.insertEmOutgoingQuality(emOutgoingQuality);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error("任务抽取失败,请联系管理员");
		}

		return AjaxResult.success();
	}


	@Override
	public AjaxResult extractEmOutgoingQualityQC(EmOutgoingQuality emOutgoingQuality) {

		try {
			// 抽取当班任务
			if (null != emOutgoingQuality.getBatchNum() && emOutgoingQuality.getBatchNum() > 0) {
				EmTask emTask = new EmTask();
				emTask.setStatus("2");
				Date[] date = DateUtils.getShiftDateQC();
				emTask.setStartTime(date[0]);
				emTask.setEndTime(date[1]);
				emTask.setSiteKey(emOutgoingQuality.getSiteKey());
				emTask.setTaskNum(emOutgoingQuality.getBatchNum());
				emTask.setHangameKey(emOutgoingQuality.getHangameKey());

				SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
				Date dates = new Date();
				String batchno =emOutgoingQuality.getSiteKey()+ format.format(dates)+"01"+"A";
				emTask.setBatchno(batchno);
				//当前批次存在的任务不抽取
				List<EmTask> emTaskList = taskService.selectRandomEmTaskListQC(emTask);

				if (emTaskList.size() > 0) {
					// 获取批次
					//根据事业部创建月度批次
					String uuid = batchno;
//					emOutgoingQuality.setUserId(emOutgoingQuality.getUserId());
					emOutgoingQuality.setBatchNo(uuid);
					emOutgoingQuality.setIsFinished("0");
					for (EmTask task : emTaskList) {
						emOutgoingQuality.setHangameKey(task.getHangameKey());
						emOutgoingQuality.setSiteKey(task.getSiteKey());
						emOutgoingQuality.setTaskId(task.getId());
						emOutgoingQualityMapper.insertEmOutgoingQuality(emOutgoingQuality);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error("任务抽取失败,请联系管理员");
		}

		return AjaxResult.success();
	}

	@Override
	public AjaxResult countCanBeExtracted(EmOutgoingQuality emOutgoingQuality) {

		EmTask emTask = new EmTask();
		emTask.setStatus("2");
		Date[] date = DateUtils.getShiftDateQC();
		emTask.setStartTime(date[0]);
		emTask.setEndTime(date[1]);
		emTask.setSiteKey(emOutgoingQuality.getSiteKey());

		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
		Date dates = new Date();
		String batchno =emOutgoingQuality.getSiteKey()+ format.format(dates)+"01"+"A";
		emTask.setBatchno(batchno);

		Integer integer = emTaskMapper.countCanBeExtracted(emTask);
		return AjaxResult.success(integer);
	}

	@Override
	public AjaxResult extractEmOutgoingQualityQA(EmOutgoingQuality emOutgoingQuality) {

		try {
			// 抽取当班任务
			if (null != emOutgoingQuality.getBatchNum() && emOutgoingQuality.getBatchNum() > 0) {
				EmTask emTask = new EmTask();
				emTask.setStatus("2");
				Date[] date = DateUtils.getShiftDateQC();
				emTask.setStartTime(date[0]);
				emTask.setEndTime(date[1]);
				emTask.setSiteKey(emOutgoingQuality.getSiteKey());
				emTask.setHangameKey(emOutgoingQuality.getHangameKey());
				emTask.setTaskNum(emOutgoingQuality.getBatchNum());

				SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
				Date dates = new Date();
				String batchno ="238"+ format.format(dates)+"01"+"C";
				emTask.setBatchno(batchno);
				//当前批次存在的任务不抽取
				List<EmTask> emTaskList = emTaskMapper.selectRandomEmTaskListQA(emTask);
				if (emTaskList.size() > 0) {
					// 获取批次
					//根据事业部创建月度批次
					String uuid = batchno;
//					emOutgoingQuality.setUserId(emOutgoingQuality.getUserId());
					emOutgoingQuality.setBatchNo(uuid);
					emOutgoingQuality.setIsFinished("0");
					for (EmTask task : emTaskList) {
						emOutgoingQuality.setHangameKey(task.getHangameKey());
						emOutgoingQuality.setSiteKey(task.getSiteKey());
						emOutgoingQuality.setTaskId(task.getId());
						emOutgoingQualityMapper.insertEmOutgoingQuality(emOutgoingQuality);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error("任务抽取失败,请联系管理员");
		}

		return AjaxResult.success();
	}

	/**
	 * LPA抽查专用
	 * @param emOutgoingQuality 设备任务检查
	 * @return
	 */
	@Override
	public AjaxResult extractEmOutgoingQualityLPA(EmOutgoingQuality emOutgoingQuality) {
		try {
			EmTask emTask = new EmTask();
			emTask.setStatus("2");
			Date[] date = DateUtils.getShiftDateLPA();
			emTask.setStartTime(date[0]);
			emTask.setEndTime(date[1]);
			emTask.setTaskType(emOutgoingQuality.getTastType());
			emTask.setDepartment(emOutgoingQuality.getHangameKey());
			emTask.setSiteKey(emOutgoingQuality.getSiteKey());
			//emTask.setHangameKey(emOutgoingQuality.getHangameKey());
			//emTask.setTaskNum(emOutgoingQuality.getBatchNum());
			List<EmTask> emTaskList= new ArrayList<>() ;
			if(emTask.getDepartment().contains("厂长")){
				emTaskList = emTaskMapper.selectEmTaskListLPASUPER(emTask);
			}
			else
			{
				 emTaskList = emTaskMapper.selectEmTaskListLPA(emTask);
			}



			if (emTaskList.size() > 0) {
				// 获取批次
				String uuid = emOutgoingQuality.getBatchNo();
				emOutgoingQuality.setBatchNo(uuid);
				emOutgoingQuality.setIsFinished("0");
				for (EmTask task : emTaskList) {
					//已完成的任务不插入
					EmOutgoingQuality eq =new EmOutgoingQuality();
					eq.setTaskId(task.getId());
					eq.setBatchNo(uuid);
						List<EmOutgoingQuality> emOutgoingQualities = emOutgoingQualityMapper.selectEmOutgoingQualityListLPA(eq);
						if (emOutgoingQualities.size() == 0) {
							emOutgoingQuality.setHangameKey(task.getHangameKey());
							emOutgoingQuality.setSiteKey(task.getSiteKey());
							emOutgoingQuality.setTaskId(task.getId());
							emOutgoingQualityMapper.insertEmOutgoingQuality(emOutgoingQuality);
						}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.error("任务抽取失败,请联系管理员");
		}
		return AjaxResult.success();
	}

}
